﻿using System;
using Microsoft.SharePoint;
#if SP_Server && !SP_07
using Microsoft.SharePoint.Taxonomy;
#endif

namespace DE.Sdc.SharePoint.SPContentStructure.Import
{
    public class DeploymentContext
    {
        private DeploymentContext _snapshot;
        public SPWeb Web { get; set; }
        public SPList List { get; set; }
        public SPFolder Folder { get; set; }
        public SPItem Item { get; set; }
        public SPFile File { get; set; }
        public SPWeb ReceiverWeb { get; set; }
        public SPFeatureReceiverProperties ReceiverProperties { get; set; }
        public String ExportPath { get; set; }
        public String ImportPath { get; set; }
        public Item ItemDefinition { get; set; }
        public Folder FolderDefinition { get; set; }
        public Web WebDefinition { get; set; }
        public WebPart WebPartDefinition { get; set; }
        public List ListDefinition { get; set; }
        public File FileDefinition { get; set; }
        public DeploymentObjects CurrentObjectType { get; set; }

        //TODO: Make this configurable
        private const string ContentStructureFilesZipName = "ContextStructureFiles.zip";

        public enum DeploymentObjects
        {
            Folder,
            Web,
            List,
            Item,
            Attachment,
            AttachmentFolder,
            Navigation,
            File
        }

        #if SP_Server && !SP_07
        public TaxonomySession Taxonomy
        {
            get { return new TaxonomySession(Web.Site); }
        }
        #endif

        public string RootUrl
        {
            get
            {
                if (Web != null)
                    return Web.ServerRelativeUrl.TrimEnd('/');
                if (List != null)
                    return List.ParentWeb.ServerRelativeUrl.TrimEnd('/');

                return null;
            }
        }

        public String WebApplicationUrl
        {
            get
            {
                if (Web != null)
                    return Web.Site.WebApplication.AlternateUrls[0].Uri.OriginalString;

                if (List != null)
                    return List.ParentWeb.Site.WebApplication.AlternateUrls[0].Uri.OriginalString;

                return null;
            }
        }

        [ThreadStatic]
        private static DeploymentContext _current;

        public static DeploymentContext Current
        {
            get { return _current ?? (_current = new DeploymentContext()); }
        }

        public void CreateSnapshot()
        {
            _snapshot = new DeploymentContext
            {
                ExportPath = ExportPath,
                File = File,
                FileDefinition = FileDefinition,
                Folder = Folder,
                FolderDefinition = FolderDefinition,
                ImportPath = ImportPath,
                Item = Item,
                ItemDefinition = ItemDefinition,
                List = List,
                ListDefinition = ListDefinition,
                ReceiverProperties = ReceiverProperties,
                ReceiverWeb = ReceiverWeb,
                Web = Web,
                WebDefinition = WebDefinition,
                WebPartDefinition = WebPartDefinition
            };
        }

        public void RestoreSnapshot(bool deleteSnapshot)
        {
            if (_snapshot == null)
                return;

            _current = _snapshot;

            if (deleteSnapshot)
                _snapshot = null;
        }

        public static void Reset()
        {
            _current = null;
        }

        public string GetContentStructureZipPath()
        {
            return (System.IO.Path.Combine(DeploymentContext.Current.ImportPath, ContentStructureFilesZipName));
        }

        public bool ExistsContentStructureZip()
        {
            return (System.IO.File.Exists(GetContentStructureZipPath()));
        }
    }
}
